﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kokomo.Mirror.PeFormat;
using Kokomo.Environment;

namespace Kokomo.PeInspector
{
	[ViewType(typeof(PeModuleStringsView))]
	class PeModuleStringsViewModel : SearchViewModel<PeModuleString>
	{
		public PeModuleStringsViewModel(
			PeModule module,
			IEnumerable<PeModuleString> moduleStrings
			)
		{
			if (module == null) throw new ArgumentNullException("module");
			if (moduleStrings == null) throw new ArgumentNullException("moduleStrings");

			this.Module = module;
			this._allModuleStrings = moduleStrings;
		}

		public PeModule Module { get; private set; }

		private IEnumerable<PeModuleString> _allModuleStrings;

		protected override IEnumerable<PeModuleString> OnSearchAsync(string searchTerm, int searchVersion)
		{
			if (string.IsNullOrEmpty(searchTerm))
			{
				return this._allModuleStrings;
			}
			else
			{
				List<PeModuleString> strings = new List<PeModuleString>();
				foreach (var str in this._allModuleStrings)
				{
					if (str.String.Contains(searchTerm, StringComparison.InvariantCultureIgnoreCase))
						strings.Add(str);
				}

				return strings;
			}
		}
	}
}
